package com.hjh.java.thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Test {

    public static void main(String[] args) {

        for (int i = 0; i <50 ; i++) {
            new Test().testMethod();

        }

    }


    ThreadPoolExecutor pool= new ThreadPoolExecutor(2,10,500, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
    Integer aValue = new Integer(10000);
    Object lock =new Object();


    public void testMethod(){
        int size = 1000;
       final CountDownLatch countDownLatch = new CountDownLatch(size);

        for (int i=0;i<size;i++){

            pool.submit(()->{add(countDownLatch);});
//            pool.submit(new Runnable() {
//                public void run() {
//                    add(countDownLatch);
//                }
//            });
        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(aValue);
    }

    public void add(CountDownLatch cd){
        synchronized (lock){
           aValue++;
           cd.countDown();
        }
    }
}
